package com.uam.core.tool;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.uam.core.entity.PointInfoData;
import com.uam.core.entity.model.ElasticsearchQueryResult;
import com.uam.core.entity.model.TaskModel;
import com.uam.core.entity.uitls.DateUtils;
import org.elasticsearch.action.DocWriteRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;

import java.util.*;

/**
 * 获取轨迹点
 * @author Administrator
 */
public class GetOldPointTool {
    public static  List<PointInfoData> getPoints (BulkRequest bulkRequest, TaskModel task){
        List<PointInfoData> data1=new ArrayList();
        List<DocWriteRequest<?>> d=bulkRequest.requests();
        int row=d.size();
        for(int j=0;j<row;j++){
            try {
                IndexRequest object = (IndexRequest) d.get(j);
                Map map = object.sourceAsMap();
                Map map1 = (Map) map.get("position");
//                String dataLat = (map1.get("lat") + "").split("\\.")[1];
//                if (dataLat.length() < 7) {
                data1.add(new PointInfoData(
                        Long.valueOf(map.get("own_time") + ""),
                        Double.valueOf(map1.get("lon") + ""),
                        Double.valueOf(map1.get("lat") + ""),
                        Double.valueOf(map.get(ElasticsearchQueryResult.CommonFields.ALTITUDE.getName()) + ""),
                        Integer.valueOf(map.get("index")+""),Double.valueOf(map.get("last_position_distance")+" "),
                        task.getName(),object.id()));
//                }
//                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        Collections.sort(data1, Comparator.comparing(PointInfoData::getTime));
        return data1;
    }

    public static PointInfoData getPointInfoData(Map<String,Object> dataMap,TaskModel task ){
       return new PointInfoData(
               Long.valueOf(dataMap.get("own_time") + ""),
               Double.valueOf(dataMap.get("lon") + ""),
               Double.valueOf(dataMap.get("lat") + ""),
               Double.valueOf(dataMap.get(ElasticsearchQueryResult.CommonFields.ALTITUDE.getName()) + ""),
               Integer.valueOf(dataMap.get("index")+""),Double.valueOf(dataMap.get("last_position_distance")+" "),
               task.getName(),dataMap.get("id")+"");
    }

    public static  void getPoints (BulkRequest bulkRequest, JSONArray jsonArray,TaskModel task){
        List<PointInfoData> data1=getPoints(bulkRequest,task);
        data1.forEach(a->{
            JSONArray jsonArrayTemp=new JSONArray();
            jsonArrayTemp.add(a.getLon());
            jsonArrayTemp.add(a.getLat());
            jsonArrayTemp.add(a.getHeight());
            jsonArrayTemp.add(a.getTime());
            jsonArrayTemp.add(a.getIndex());
            jsonArray.add(jsonArrayTemp);
        });
    }

    public static  JSONArray getPoints(List<PointInfoData> data1){
        JSONArray jsonArray=new JSONArray(data1.size());
        data1.forEach(a->{
            JSONArray jsonArrayTemp=new JSONArray();
            jsonArrayTemp.add(a.getLon());
            jsonArrayTemp.add(a.getLat());
            jsonArrayTemp.add(a.getHeight());
            jsonArrayTemp.add(a.getTime());
            jsonArrayTemp.add(a.getIndex());
            jsonArray.add(jsonArrayTemp);
        });
        return jsonArray;
    }

    public static  void getPoints(BulkRequest bulkRequest, List<PointInfoData> dataList, TaskModel task){
        dataList.addAll(getPoints(bulkRequest,task));
    }
}
